package sch.jl.caigou.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import sch.jl.caigou.dao.Dao;
import sch.jl.caigou.db.JDBCUtils;
import sch.jl.caigou.utils.ReflectionUtils;

public class DaoImpl<T> implements Dao<T> {

	private QueryRunner queryRunner = new QueryRunner();
	private Class<T> clazz;
	public DaoImpl()
	{
		clazz = ReflectionUtils.getSuperGenericType(getClass());
	}
	
	public long insert(String sql,Object...args)
	{
		long id = 0;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try
		{
			connection = JDBCUtils.getConnection();
			preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			if(args!=null)
			{
				for(int i=0;i<args.length;i++)
				{
					preparedStatement.setObject(i+1, args[i]);
				}
			}
			preparedStatement.executeUpdate();
			resultSet = preparedStatement.getGeneratedKeys();
			if(resultSet.next())
			{
				id = resultSet.getLong(1);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			JDBCUtils.release(resultSet, preparedStatement);
			JDBCUtils.release(connection);
		}
		return id;
	}
	public void update(String sql,Object...args)
	{
		Connection connection = null;
		try
		{
			connection = JDBCUtils.getConnection();
			queryRunner.update(connection, sql, args);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			JDBCUtils.release(connection);
		}
	}
	
	@SuppressWarnings("unchecked")
	public <V> V getSingleVal(String sql, Object... args) {
		Connection connection = null;
		try
		{
			connection = JDBCUtils.getConnection();
			return (V)queryRunner.query(connection, sql, new ScalarHandler(), args);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			JDBCUtils.release(connection);
		}
		return null;
	}
	public T query(String sql,Object...args)
	{
		Connection connection = null;
		try
		{
			connection = JDBCUtils.getConnection();
			return queryRunner.query(connection, sql, new BeanHandler<T>(clazz), args);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			JDBCUtils.release(connection);
		}
		return null;
	}
	
	public List<T> queryForList(String sql,Object...args)
	{
		Connection connection = null;
		try
		{
			connection = JDBCUtils.getConnection();
			return queryRunner.query(connection, sql,new BeanListHandler<T>(clazz), args);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			JDBCUtils.release(connection);
		}
		return null;
	}
	
	public void delete(String sql, Object... args)
	{
		Connection connection = null;
		try
		{
			connection = JDBCUtils.getConnection();
			PreparedStatement pst = connection.prepareStatement(sql);
			pst.executeUpdate();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			JDBCUtils.release(connection);
		}
	}

}
